package com.seocoo.worker.order.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seocoo.worker.R;
import com.seocoo.worker.base.menu.NavigationTextButton;
import com.seocoo.worker.order.OrderStarInstallParentFragment;
import com.seocoo.worker.order.OrderStarMaintainParentFragment1;

import java.util.List;

/**
 * author : Ssp
 * date   : 2020/2/23 15:51
 * desc   : 文本tab切换
 */
public class NavTextFragment extends Fragment implements View.OnClickListener {

    private NavigationTextButton tv_install_order;
    private NavigationTextButton tv_maintain_order;

    private Context mContext;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationTextButton mCurrentNavButton;
    private OnNavigationReselectListener mOnNavigationReselectListener;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_fragment_nav_text, container);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData(view);
    }

    public void initData(View view) {
        tv_install_order = view.findViewById(R.id.tv_install_order);
        tv_maintain_order = view.findViewById(R.id.tv_maintain_order);
        tv_install_order.init("安装订单", OrderStarInstallParentFragment.class);
        tv_maintain_order.init("维修订单", OrderStarMaintainParentFragment1.class);
        tv_install_order.setOnClickListener(this);
        tv_maintain_order.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        NavigationTextButton nav = (NavigationTextButton) v;
        doSelect(nav);
    }

    private void doSelect(NavigationTextButton newNavButton) {

        NavigationTextButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;
    }

    private void onReselect(NavigationTextButton navigationButton) {
        OnNavigationReselectListener listener = mOnNavigationReselectListener;
        if (listener != null) {
            listener.onReselect(navigationButton);
        }
    }

    private void doTabChanged(NavigationTextButton oldNavButton, NavigationTextButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(mContext,
                        newNavButton.getClx().getName(), null);
                ft.add(mContainerId, fragment, newNavButton.getTag());
                newNavButton.setFragment(fragment);
            } else {
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commitAllowingStateLoss();
    }

    public interface OnNavigationReselectListener {
        void onReselect(NavigationTextButton navigationButton);
    }

    public void setup(Context context, FragmentManager fragmentManager, int contentId, OnNavigationReselectListener listener) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        mOnNavigationReselectListener = listener;

        // do clear
        clearOldFragment();
        // do select first
        doSelect(tv_install_order);
    }

    @SuppressWarnings("RestrictedApi")
    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0) {
            return;
        }
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this && fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit) {
            transaction.commitNow();
        }
    }
}
